package com.zjy.heath_ms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mysql.jdbc.StringUtils;
import com.zjy.heath_ms.constant.MessageConstant;
import com.zjy.heath_ms.entity.QueryPageBean;
import com.zjy.heath_ms.entity.Result;
import com.zjy.heath_ms.mapper.MemberMapper;
import com.zjy.heath_ms.pojo.Member;
import com.zjy.heath_ms.service.MemberService;
import com.zjy.heath_ms.utils.MD5Utils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 项目名 heath_ms
 * 类名  MemberServiceImpl
 * 作者 lzq
 * 创建时间  2025/5/18 16:30
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {
    @Override
    public Page<Member> findPage(QueryPageBean queryPageBean) {
        // 查询条件
        LambdaQueryWrapper<Member> memberLambdaQueryWrapper = new LambdaQueryWrapper<>();
        String queryString = queryPageBean.getQueryString();
        if (!StringUtils.isNullOrEmpty(queryString)) {
            memberLambdaQueryWrapper.like(Member::getFileNumber, queryString)
                    .or()
                    .like(Member::getName, queryString)
                    .or()
                    .like(Member::getPhoneNumber, queryString);
        }
        memberLambdaQueryWrapper.isNotNull(Member::getFileNumber);
        memberLambdaQueryWrapper.orderByDesc(Member::getRegTime);
        // 分页条件
        Page<Member> memberPage = new Page<>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        // 执行查询
        Page<Member> result = baseMapper.selectPage(memberPage, memberLambdaQueryWrapper);

        return result;
    }

    @Override
    public Result add(Member member) {
        String idCard = member.getIdCard();
        member.setRegTime(new Date());
        member.setPassword(MD5Utils.md5(idCard.substring(idCard.length()-6)));
        boolean flag = baseMapper.insert(member) == 1;
        return new Result(flag, flag ? MessageConstant.ADD_MEMBER_SUCCESS : MessageConstant.ADD_MEMBER_FAIL);
    }

    @Override
    public Result edit(Member member) {
        boolean flag = baseMapper.updateById(member) == 1;
        return new Result(flag, flag ? MessageConstant.EDIT_MEMBER_SUCCESS : MessageConstant.EDIT_MEMBER_FAIL);
    }

    @Override
    public Result delete(Integer id) {
        // 删除订单信息
//        orderMapper.delete(new LambdaQueryWrapper<Order>().eq(Order::getMemberId, id));
        // 删除会员信息
        boolean flag = baseMapper.deleteById(id) == 1;
        return new Result(flag, flag ? MessageConstant.DELETE_MEMBER_SUCCESS :MessageConstant.DELETE_MEMBER_FAIL);
    }

}
